package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.DeviceOptions;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.constant.UserOptions;
import com.hzhr.iot.device.convertor.custom.IotDeviceCustomConvertor;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.dao.helper.IotDeviceDaoHelper;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.entity.ext.IotDeviceEntityExt;
import com.hzhr.iot.device.model.iotDevice.IotDeviceGetModel;
import com.hzhr.iot.device.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.model.iotDevice.IotDevicePutModel;
import com.hzhr.iot.device.model.pressureMonitor1.PressureMonitor1GetModel;
import com.hzhr.iot.device.model.pressureMonitor1.PressureMonitor1Model;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.device.service.helper.CollectDeviceServiceHelper;
import com.hzhr.iot.device.service.helper.User_CompanyServiceHelper;
import com.hzhr.iot.device.service.helper.User_DeptServiceHelper;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.user.sdk.model.company.CompanyModel;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.model.dept_name.Dept_NameModel;
import com.hzhr.iot.user.sdk.service.User_CompanyService;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import com.hzhr.iot.user.sdk.service.User_Dept_NameService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

@Service
@CygService(entity = IotDeviceEntity.class, model = PressureMonitor1Model.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),
        @CygQuery(name = "_equal_deviceNo", query = "equal_code", type = String.class),
        @CygQuery(name = "_greaterthanorequal_installTime", query = "greaterthanorequal_installTime", type = Date.class),
        @CygQuery(name = "_lessthan_installTime", query = "lessthan_installTime", type = Date.class),


})
public class PressureMonitor1Service extends BaseService {
    @Autowired
    IotDeviceCustomConvertor iotDeviceCustomConvertor;
    @Autowired
    IotDeviceDao iotDeviceDao;
    @Autowired
    User_DeptService user_deptService;

    //region javadoc注释 : get
    /**
     * 查询加装在用户管辖范围内的调压箱柜上的压力检测仪 <br/><br/>
     * 步骤 : <br/>
     * 1. 查询压力监测仪：查询加装在用户管辖范围内的调压箱柜上的压力检测仪 <br/>
     * 2. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_dept : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示部门信息;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.id = #{_equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_deviceNo : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.deviceNo = #{_equal_deviceNo};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_installAddress : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.installAddress like #{_like_installAddress} + '%';<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _greaterthanorequal_installTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.installTime >= #{_greaterthanorequal_installTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/>
     * _lessthan_installTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.installTime < #{_lessthan_installTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/><br/>
     * @throws ServiceException <br/>
     * @see PressureMonitor1GetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询加装在用户管辖范围内的调压箱柜上的压力检测仪
    //步骤 :
    //1. 查询压力监测仪：查询加装在用户管辖范围内的调压箱柜上的压力检测仪
    //2. 返回信息
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_dept", value = "是否展示部门信息", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where PressureMonitor1Model.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_deviceNo", value = "筛选条件 : where PressureMonitor1Model.deviceNo = #{equal_deviceNo}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_installAddress", value = "筛选条件 : where PressureMonitor1Model.installAddress like #{like_installAddress} + '%'",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "greaterthanorequal_installTime", value = "筛选条件 : where PressureMonitor1Model.installTime >= #{greaterthanorequal_installTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "lessthan_installTime", value = "筛选条件 : where PressureMonitor1Model.installTime < #{lessthan_installTime}",required = false, dataType = "long", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_dept", value = "是否展示部门信息", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where PressureMonitor1Model.id = #{_equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_deviceNo", value = "筛选条件 : where PressureMonitor1Model.deviceNo = #{_equal_deviceNo}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_installAddress", value = "筛选条件 : where PressureMonitor1Model.installAddress like #{_like_installAddress} + '%'",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthanorequal_installTime", value = "筛选条件 : where PressureMonitor1Model.installTime >= #{_greaterthanorequal_installTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "_lessthan_installTime", value = "筛选条件 : where PressureMonitor1Model.installTime < #{_lessthan_installTime}",required = false, dataType = "long", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/pressure-monitor1s", method = RequestMethod.GET)
    //endregion
    public PressureMonitor1GetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 查询压力监测仪：查询加装在用户管辖范围内的调压箱柜上的压力检测仪 */
        IotDeviceCustomQueryModel queryModel = iotDeviceCustomConvertor.pressureMonitor1Service_query2queryModel(query);
        String _like_installAddress = HzhrTypeUtil.object2string(query.get("_like_installAddress"));
        if (!StringUtils.isEmpty(_like_installAddress)) {
            queryModel.setLike_collectDeviceExt_installAddress(_like_installAddress + "%");
        }
        queryModel.setEqual_categoryCode("5");//压力监测仪
        queryModel.setCustom3("tiaoYaXiangGui");//采集设备为调压箱或调压柜
        String userCode = getUserCode(header);
        queryModel.setLike_collectDeviceExt_resourceCode(userCode + "%");

        List<IotDeviceEntityExt> extList = null;
        String _show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(_show)) {
            extList = iotDeviceDao.selectExtList(queryModel);
        }
        Integer total = null;
        Boolean _show_total = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (_show_total != null && _show_total) {
            total = iotDeviceDao.selectCount(queryModel);
        }

        /* 返回信息 */
        PressureMonitor1GetModel result = iotDeviceCustomConvertor.extList2pressureMonitor1GetModel(extList, total, null, query);
        //写入额外信息
        PressureMonitor1Model model = result.getModel();
        if (model != null) {
            Boolean _show_dept = HzhrTypeUtil.object2boolean(query.get("_show_dept"));
            if (_show_dept != null && _show_dept) {//写入部门信息
                Integer deptId = model.getDeptId();
                if (deptId != null) {
                    Map<String, Object> query1 = new HashMap<>();
                    query1.put("_show_managementName",true);
                    query1.put("_show_subCompanyName",true);
                    query1.put("_show", "one");
                    query1.put("_equal_id", deptId);
                    DeptModel deptModel = user_deptService.get(initHeader(header, AccessModeEnum.GLOBAL), query1).getModel();

                    if (deptModel != null) {
                        model.setManagementName(deptModel.getManagementName());
                        model.setSectionName(deptModel.getSubCompanyName());
                    }
                }
            }
        }
        List<PressureMonitor1Model> models = result.getModelList();
        if (models != null && models.size() > 0) {
            Boolean _show_dept = HzhrTypeUtil.object2boolean(query.get("_show_dept"));
            if (_show_dept != null && _show_dept) {//写入部门信息
                for (PressureMonitor1Model model1 :
                     models) {
                    Integer deptId = model1.getDeptId();
                    if (deptId != null) {
                        Map<String, Object> query1 = new HashMap<>();
                        query1.put("_show_managementName",true);
                        query1.put("_show_subCompanyName",true);
                        query1.put("_show", "one");
                        query1.put("_equal_id", deptId);
                        DeptModel deptModel = user_deptService.get(initHeader(header, AccessModeEnum.GLOBAL), query1).getModel();

                        if (deptModel != null) {
                            model1.setManagementName(deptModel.getManagementName());
                            model1.setSectionName(deptModel.getSubCompanyName());
                        }
                    }
                }
            }
        }

        return result;
    }
}

